// pages/c-fulltext/c-fulltext.js
const api = require('../../utils/api')
import {formatTime} from '../../utils/util'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isShowComment: false, // 进行评论
    // 评论确定按钮
    isShowPublish: false,
    confirmButton: [{text: '取消'}, {text: '确定'}],
    content: '', // 评论内容
    card: {},
    isThumbsUp: false, // 默认未点赞未收藏
    isCollect: false,
    thumbsSrc: "../../images/community/thumbs.png",
    collectionSrc: "../../images/community/collection.png",
  },
  handleComment() {
    const {isShowComment} = this.data
    this.setData({isShowComment: !isShowComment})
  },
  showPublish() {    
    this.setData({
      isShowPublish: true
    })
  },
  hidePublish(event) {
    this.setData({
      isShowPublish: false
    })
    const isConfirm = event.detail.index
    if (isConfirm == 1) {
      const {content, isShowComment} = this.data
      if (!content) { // 未评论
        wx.showToast({
          title: '请填写评论内容',
          icon: 'none',
          duration: 1000
        })
        return
      }
      // 将评论保存到数据库，并清空 comment，收回评论界面
      const comment = {}
      const date = new Date()
      comment.publishTime = formatTime(date)
      comment.content = content
      comment._id = this.data.card._id // 评论对应的帖子 _id
      // console.log(comment);
      api.addComment(comment, () => {
        // 添加完评论，清除评论内容，隐藏评论框
        this.setData({content: '', isShowComment: !isShowComment})
        // 添加完评论就重新获取帖子数据
        api.getCardById(this.data.card._id).then(card => {
          this.setData({card})
          // console.log('card', card);
        })
      })
    }
  },
  formSubmit(e) {
    const {content} = e.detail.value
    this.setData({content})
  },
  // 点赞
  thumbsUp() {
    const flag = this.data.isThumbsUp
    const { _id } = this.data.card
    if (!flag) { // 未点赞，进行点赞
      this.setData({
        isThumbsUp: true,
        thumbsSrc: "../../images/community/thumbsH.png"
      })
      api.dianzan(_id)
    } else { // 已经点赞，取消点赞
      this.setData({
        isThumbsUp: false,
        thumbsSrc: "../../images/community/thumbs.png"
      })
      api.cancelDianzan(_id)
    }
  },
  // 收藏
  collect() {
    const flag = this.data.isCollect
    const { _id } = this.data.card
    if (!flag) { // 未收藏，进行收藏
      this.setData({
        isCollect: true,
        collectionSrc: "../../images/community/collectionH.png"
      })
      api.shoucang(_id)
    } else { // 已经收藏，取消收藏
      this.setData({
        isCollect: false,
        collectionSrc: "../../images/community/collection.png"
      })
      api.cancelShoucang(_id)
    }
  },
  // 页面加载时，判断点赞和收藏与否
  // 此帖子是否在 cards 中
  isOwn(cards, id) {
    // cards 中，有一项，其 id 等于 id 就返回 true
    return cards.some(card => {
      // console.log(card.id, id, card.id === id);
      return card.id === id
    })
  },
  // 判断是否点赞
  // 判断是否收藏
  isHigh() {
    let { collectionSrc, thumbsSrc } = this.data
    api.getInfo(data => {
      // console.log('this.data.card._id', this.data.card._id);
      const id = this.data.card._id || this.data.card.id
      // console.log('id',id)
      // console.log('data', data);
      const { collection, thumbcards } = data
      // console.log('collection')
      if (this.isOwn(collection, id)) {
        collectionSrc = "../../images/community/collectionH.png"
        this.setData({ isCollect: true, collectionSrc })
      }
      // console.log('thumbcards')
      if (this.isOwn(thumbcards, id)) {
        thumbsSrc = "../../images/community/thumbsH.png"
        this.setData({ isThumbsUp: true, thumbsSrc })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('sendData', (card) => {
      this.setData({card})
      // console.log(card._id)
      api.read({_id: card._id})
      this.isHigh()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.isHigh()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})